package com.itcam.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Order;
import org.springframework.stereotype.Component;

/**
 * Author:Cammy.Wu
 * Date:2024-04-25
 * Description:使用普通方式进行事务添加
 */

@Component
@Aspect
@Order(10) // 指定一个优先级的值，值越小 优先级越高！优先级越高则前置先执行，后置后执行
public class TxAdvice {
    @Before("com.itcam.pointcut.MyPointCut.lpc()")
    // JoinPoint获取目标方法的信息
    public void begin(JoinPoint joinPoint) {
        System.out.println("开启事务...");
    }

    @AfterReturning(value = "com.itcam.pointcut.MyPointCut.lpc()", returning = "result")
    // Object类型的形参：获取返回值方法的信息
    public void commit(Object result) {
        System.out.println("事务提交...");
    }

    @AfterThrowing(value = "com.itcam.pointcut.MyPointCut.lpc()", throwing = "throwable")
    // Throwable类型的形参：记录异常信息 JoinPoint的形参可以知道哪个类发生的什么异常
    public void rollback(JoinPoint joinPoint, Throwable throwable) {
        System.out.println("事务回滚...");
    }
}
